<?php

// 参考：http://www.php.net/manual/zh/pdo.connections.php
// https://segmentfault.com/a/1190000004106175

include_once 'db_config.php';
try {
// array(PDO::ATTR_PERSISTENT => true) 开启持久化连接
    $dbh = new PDO('mysql:host=localhost;dbname='.$db['db_name'], $db['user'], $db['pwd'], array(PDO::ATTR_PERSISTENT => true));
    //  页面编码的声明,否则遇到中文会出现乱码
    $dbh->query('set names utf8;');
    if (empty($_POST["date_start"]) & empty($_POST["date_end"])) {
        // 获取指定日期的净增用户总量
        $next_day = date("Y-m-d", strtotime('-7 days'));
        $end_day = date('Y-m-d', strtotime('-1 days'));
    } else {
        $next_day = $_POST['date_start'];
        $end_day = $_POST['date_end'];
    }
    $arr = array();
    $sql = 'SELECT * FROM `articlesummary` where ref_date>="' . $next_day . '" and ref_date<= "' . $end_day . '" order by title';

    foreach ($dbh->query($sql) as $row) {
        $arr['date'][] = $row['ref_date'];
//        $sub_sql = 'SELECT title,sum(int_page_read_user) as iru,sum(int_page_read_count) as irc,sum(ori_page_read_user) as oru,sum(ori_page_read_count) as orc,sum(share_user) as su,sum(share_count) as sc,sum(add_to_fav_user) as afu,sum(add_to_fav_count) as afc FROM `articlesummary` where msgid="' . $result['msgid'] . '" ';
//        foreach ($dbh->query($sub_sql) as $key => $row) {
//            var_dump($result);
            // 把日期和用户数单独组成列 | 变量追加到数组中
//            $arr['date'][] = $row['ref_date'];
            $arr['title'][] = $row['title'];
            $arr['display_title'][] = '['.$row['ref_date'].']'.$row['title'];
//            $arr['msgid'][] = $row['msgid'];
            $arr['ir_user'][] = $row['int_page_read_user'];
//            $arr['ir_count'][] = $row['irc'];
            $arr['or_user'][] = $row['ori_page_read_user'];
//            $arr['or_count'][] = $row['orc'];
//            $arr['s_scene'][] = $row['share_scene'];
            $arr['s_user'][] = $row['share_user'];
//            $arr['s_count'][] = $row['sc'];
            $arr['f_user'][] = $row['add_to_fav_user'];
//            $arr['f_count'][] = $row['afc'];
//        }
    }
    // 把数据转换成json格式
    $data = json_encode($arr);
    echo $data;

    // 现在运行完成，在此关闭连接
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>